package com.sky.orangehireserver.modules.enterprise.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.orangehireserver.aspect.ApiOperationLog;
import com.sky.orangehireserver.modules.enterprise.dto.JobCreateDTO;
import com.sky.orangehireserver.modules.enterprise.dto.JobQueryDTO;
import com.sky.orangehireserver.modules.enterprise.dto.JobUpdateDTO;
import com.sky.orangehireserver.modules.enterprise.service.EnterpriseJobService;
import com.sky.orangehireserver.modules.enterprise.vo.JobVO;
import com.sky.orangehireserver.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业岗位管理控制器
 * 
 * @author sky
 */
@Slf4j
@RestController
@RequestMapping("/enterprise/jobs")
@RequiredArgsConstructor
@Tag(name = "企业岗位管理", description = "企业HR管理岗位相关接口")
public class EnterpriseJobController {

    private final EnterpriseJobService enterpriseJobService;

    /**
     * 创建岗位
     * 
     * @param orgId 企业ID（从请求头或上下文中获取）
     * @param ownerId 职位负责人ID（当前用户ID）
     * @param createDTO 岗位创建请求DTO
     * @return 岗位信息VO
     */
    @PostMapping
    @Operation(summary = "创建岗位", description = "企业HR创建新岗位")
    @ApiOperationLog(description = "创建岗位")
    public Response<JobVO> createJob(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "当前用户ID", hidden = true) @RequestHeader(value = "X-User-Id", required = false) Long ownerId,
            @Valid @RequestBody JobCreateDTO createDTO) {
        
        // TODO: 在实际项目中，企业ID和用户ID应该从JWT token或用户上下文中获取
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        if (ownerId == null) {
            throw new RuntimeException("用户ID不能为空，请在请求头中添加X-User-Id");
        }
        
        JobVO jobVO = enterpriseJobService.createJob(orgId, ownerId, createDTO);
        return Response.success(jobVO);
    }

    /**
     * 更新岗位信息
     * 
     * @param orgId 企业ID
     * @param updateDTO 岗位更新请求DTO
     * @return 岗位信息VO
     */
    @PutMapping
    @Operation(summary = "更新岗位信息", description = "企业HR更新岗位信息")
    @ApiOperationLog(description = "更新岗位信息")
    public Response<JobVO> updateJob(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Valid @RequestBody JobUpdateDTO updateDTO) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        JobVO jobVO = enterpriseJobService.updateJob(orgId, updateDTO);
        return Response.success(jobVO);
    }

    /**
     * 删除岗位
     * 
     * @param orgId 企业ID
     * @param id 岗位ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除岗位", description = "企业HR删除岗位")
    @ApiOperationLog(description = "删除岗位")
    public Response<Void> deleteJob(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "岗位ID") @PathVariable Long id) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        enterpriseJobService.deleteJob(orgId, id);
        return Response.success();
    }

    /**
     * 获取岗位详情
     * 
     * @param orgId 企业ID
     * @param id 岗位ID
     * @return 岗位信息VO
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取岗位详情", description = "根据ID获取岗位详细信息")
    @ApiOperationLog(description = "获取岗位详情")
    public Response<JobVO> getJobById(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "岗位ID") @PathVariable Long id) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        JobVO jobVO = enterpriseJobService.getJobById(orgId, id);
        return Response.success(jobVO);
    }

    /**
     * 分页查询企业岗位列表
     * 
     * @param orgId 企业ID
     * @param queryDTO 查询条件DTO
     * @return 分页岗位信息
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询企业岗位列表", description = "根据条件分页查询企业岗位列表")
    @ApiOperationLog(description = "分页查询企业岗位列表")
    public Response<IPage<JobVO>> getJobPage(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Valid JobQueryDTO queryDTO) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        IPage<JobVO> jobPage = enterpriseJobService.getJobPage(orgId, queryDTO);
        return Response.success(jobPage);
    }

    /**
     * 获取企业所有岗位列表
     * 
     * @param orgId 企业ID
     * @return 岗位信息列表
     */
    @GetMapping("/all")
    @Operation(summary = "获取企业所有岗位列表", description = "获取企业所有岗位列表（不分页）")
    @ApiOperationLog(description = "获取企业所有岗位列表")
    public Response<List<JobVO>> getAllJobs(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        List<JobVO> jobs = enterpriseJobService.getAllJobsByOrgId(orgId);
        return Response.success(jobs);
    }

    /**
     * 发布岗位
     * 
     * @param orgId 企业ID
     * @param id 岗位ID
     * @return 操作结果
     */
    @PutMapping("/{id}/publish")
    @Operation(summary = "发布岗位", description = "将草稿状态的岗位发布为开放状态")
    @ApiOperationLog(description = "发布岗位")
    public Response<Void> publishJob(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "岗位ID") @PathVariable Long id) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        enterpriseJobService.publishJob(orgId, id);
        return Response.success();
    }

    /**
     * 关闭岗位
     * 
     * @param orgId 企业ID
     * @param id 岗位ID
     * @return 操作结果
     */
    @PutMapping("/{id}/close")
    @Operation(summary = "关闭岗位", description = "关闭正在招聘的岗位")
    @ApiOperationLog(description = "关闭岗位")
    public Response<Void> closeJob(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "岗位ID") @PathVariable Long id) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        enterpriseJobService.closeJob(orgId, id);
        return Response.success();
    }



    /**
     * 批量删除岗位
     * 
     * @param orgId 企业ID
     * @param ids 岗位ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除岗位", description = "批量删除多个岗位")
    @ApiOperationLog(description = "批量删除岗位")
    public Response<Void> batchDeleteJobs(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "岗位ID列表") @RequestBody List<Long> ids) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        enterpriseJobService.batchDeleteJobs(orgId, ids);
        return Response.success();
    }



    /**
     * 根据负责人获取岗位列表
     * 
     * @param orgId 企业ID
     * @param ownerId 负责人ID
     * @return 岗位信息列表
     */
    @GetMapping("/owner/{ownerId}")
    @Operation(summary = "根据负责人获取岗位列表", description = "获取指定负责人的所有岗位")
    @ApiOperationLog(description = "根据负责人获取岗位列表")
    public Response<List<JobVO>> getJobsByOwner(
            @Parameter(description = "企业ID", hidden = true) @RequestHeader(value = "X-Org-Id", required = false) Long orgId,
            @Parameter(description = "负责人ID") @PathVariable Long ownerId) {
        
        if (orgId == null) {
            throw new RuntimeException("企业ID不能为空，请在请求头中添加X-Org-Id");
        }
        
        List<JobVO> jobs = enterpriseJobService.getJobsByOwnerId(orgId, ownerId);
        return Response.success(jobs);
    }

    /**
     * 增加岗位浏览次数
     * 
     * @param id 岗位ID
     * @return 操作结果
     */
    @PutMapping("/{id}/view")
    @Operation(summary = "增加岗位浏览次数", description = "记录岗位被浏览")
    @ApiOperationLog(description = "增加岗位浏览次数")
    public Response<Void> incrementViewsCount(@Parameter(description = "岗位ID") @PathVariable Long id) {
        enterpriseJobService.incrementViewsCount(id);
        return Response.success();
    }

    /**
     * 增加岗位投递次数
     * 
     * @param id 岗位ID
     * @return 操作结果
     */
    @PutMapping("/{id}/apply")
    @Operation(summary = "增加岗位投递次数", description = "记录岗位被投递")
    @ApiOperationLog(description = "增加岗位投递次数")
    public Response<Void> incrementApplicationsCount(@Parameter(description = "岗位ID") @PathVariable Long id) {
        enterpriseJobService.incrementApplicationsCount(id);
        return Response.success();
    }
}
